package march.view 
{
	import flash.display.Sprite;
	import flash.ui.Keyboard;
	import interfaces.IOperate;
	import march.MarchC;
	import march.model.ResultVO;
	import morn.core.components.Clip;
	import org.airmvc.Dispatcher;
	import ui.march.ResultUI;
	/**
	 * ...
	 * @author WLDragon 2014-04-24
	 */
	public class Result extends ResultUI implements IOperate
	{
		
		public function Result() 
		{
			arrow = new Clip("png.common.clip_arrow", 1, 2);
			arrow.mouseChildren = false;
			arrow.mouseEnabled = false;
			arrow.frame = 1;
			arrow.y = -16;
			addChild(arrow);
			
			generalsPanel = new Sprite();
			generalsPanel.x = 75;
			generalsPanel.y = 30;
			addChild(generalsPanel);
			
		}
		
		public function update(attack:ResultVO,defense:ResultVO):void
		{
			boxWords.visible = true;
			
			btAttackName.text = attack.name;
			btAttackResult.text = attack.result;
			btAttackSB.text = String(attack.lossSoldiers);
			btAttackSJ.text = String(attack.lossGenerals);
			btAttackXJ.text = String(attack.addGenerals);
			
			btDefenseName.text = defense.name;
			btDefenseResult.text = defense.result;
			btDefenseSB.text = String(defense.lossSoldiers);
			btDefenseSJ.text = String(defense.lossGenerals);
			btDefenseXJ.text = String(defense.addGenerals);
		}
		
		public function chooseGeneral(gs:Array):void
		{
			Kyb.setOperater(this);
			generalList = [];
			boxWords.visible = false;
			//generalsPanel.visible = true;
			
			var g:GeneralItem;
			var i:int;
			for (i = 0; i < gs.length; i++) 
			{
				g = new GeneralItem(gs[i]);
				g.x = 145 * (i % 3);
				g.y = 35 * (i/3>>0);
				generalsPanel.addChild(g);
				generalList.push(g);
			}
			g = new GeneralItem(null);
			g.x = 145 * (i % 3);
			g.y = 35 * (i/3>>0);
			generalsPanel.addChild(g);
			generalList.push(g);
			index = 0;
		}
		
		public function execute(keyCode:int) : void
		{
			switch (keyCode) 
			{
				case Keyboard.A:
					index--;
				break;
				case Keyboard.D:
					index++;
				break;
				case Keyboard.W:
					index -= 3;
				break;
				case Keyboard.S:
					index += 3;
				break;
				case Keyboard.SPACE:
					if (curItem.data == null)
					{
						var a:Array = [];
						for each(var g:GeneralItem in generalList)
						{
							if (g.selected)
								a.push(g.data);
						}
						Dispatcher.send(MarchC.RETREAT_GENERALS, this, a);
						while (generalsPanel.numChildren > 0)
						{
							generalsPanel.removeChildAt(0);
						}
						arrow.y = -16;
					}
					else
						curItem.selected = !curItem.selected;
				break;
			}
		}
		
		public function get index():int 
		{
			return _index;
		}
		
		public function set index(value:int):void 
		{
			if (value < 0 || value >= generalList.length)
				return;
			
			curItem = generalList[value];
			arrow.x = curItem.x + 75 - 16;
			arrow.y = curItem.y + 34;
			
			_index = value;
		}
		
		private var arrow:Clip;
		private var _index:int;
		private var generalList:Array;
		private var curItem:GeneralItem;
		private var generalsPanel:Sprite;
	}

}